package middlewares

import (
	"github.com/gin-gonic/gin"
)

func PaginationMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req struct {
			Page     int `form:"page"`
			PageSize int `form:"page_size"`
		}
		// 设置默认值
		if req.Page == 0 {
			req.Page = 1
		}
		if req.PageSize == 0 {
			req.PageSize = 10
		}
		// 存储到上下文中
		c.Set("pageRequest", gin.H{
			"page":     req.Page,
			"pageSize": req.PageSize,
			"offset":   (req.Page - 1) * req.PageSize,
		})
		c.Next()
	}
}
